import pika
import json

def process_order(ch, method, properties, body):
    try:
        retry_count = properties.headers.get('x-retry-count', 0)
        max_attempts = properties.headers.get('x-max-attempts', 3)
        print(retry_count)
        print(max_attempts)
        1/ 0

        # 假设出错，不告知代理
        # ch.basic_ack(delivery_tag=method.delivery_tag)
    except Exception as e:
            print("达到最大重试次数，可以将消息记录到日志或进行其他处理，写入日志等操作")



# RabbitMQ 连接信息
connection_params = pika.ConnectionParameters(
    host='localhost',  # RabbitMQ 服务器地址
    virtual_host='order_vhost',  # 使用的虚拟主机
    credentials=pika.PlainCredentials('admin', '123')  # RabbitMQ 用户名和密码
)

# 创建连接和信道
connection = pika.BlockingConnection(connection_params)
channel = connection.channel()

# 声明队列
channel.queue_declare(queue='order_queue', durable=True)

# 设置消费者处理消息的回调函数
channel.basic_consume(queue='order_queue', on_message_callback=process_order)

# 开始消费消息
print("等待消息。按下 CTRL+C 退出")
try:
    channel.start_consuming()
except KeyboardInterrupt:
    channel.stop_consuming()

# 关闭连接
connection.close()
